package com.xxx.modules.controller;;
import com.alipay.api.domain.Product;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxx.modules.entity.Subject;
import com.xxx.modules.mapper.CollegeMapper;
import com.xxx.modules.mapper.SubjectMapper;
import com.xxx.modules.service.CollegeService;
import com.xxx.modules.entity.College;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import com.github.pagehelper.PageInfo;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import com.xxx.modules.utils.Result;
import com.xxx.modules.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * college
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-04-29
 */
@RestController
@RequestMapping("/common/college")
@Api(tags="college")
public class CollegeController {
    @Autowired
    private CollegeService collegeService;

    /**
    *  获取所有院校信息
    * @param pageNum
    * @param pageSize
    * @param college
    * @return
    */

    @GetMapping("/getCollegeInfo")
    @ApiOperation("获取所有院校信息")
    public Result getCollegeInfo(College college, @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value="pageSize",defaultValue = "10") Integer pageSize){
        PageInfo<College> data = collegeService.selectCollegeInfo(college,pageNum,pageSize);
        return ResultUtil.success(1,"成功",data);
    }

    /**
    * 保存院校信息
    * @param college
    * @return
    */
    @PostMapping("/saveCollegeInfo")
    @ApiOperation("保存院校信息")
    public Result saveCollegeInfo(@RequestBody College college){
        collegeService.saveCollegeInfo(college);
        return ResultUtil.success(1,"成功",null);
    }


    /**
     * 更新院校信息
     * @param college
     * @return
     */

    @PutMapping("/updateCollegeInfo")
    @ApiOperation("更新院校信息")
    public Result updateCollegeInfo(@RequestBody College college){
        collegeService.updateCollegeInfo(college);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id删除院校信息
     * @param id
     * @return
     */
    @DeleteMapping("/delCollegeInfo")
    @ApiOperation("根据id删除院校信息")
    public Result delCollegeInfo(Integer id){
        collegeService.delCollegeInfo(id);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id集合批量删除院校信息
     * @param idList
     * @return
     */
    @DeleteMapping("/delBatchCollegeInfo")
    @ApiOperation("根据id集合批量删除院校信息")
    public Result delBatchCollegeInfo(String idList){
        collegeService.delBatchCollegeInfo(idList);
        return ResultUtil.success(1,"成功",null);
    }
    @Autowired
    private CollegeMapper collegeMapper;
    @Autowired
    private SubjectMapper subjectMapper;

    @GetMapping("/getCollegeAndSubject")
    public Result getSortCar(){
        try {
            List<College> list = collegeMapper.selectList(null);
            for (College college:list){
                QueryWrapper<Subject> wrapper = new QueryWrapper<>();
                wrapper.eq("college_id",college.getId());
                List<Subject> subjectList = subjectMapper.selectList(wrapper);
                college.setChildren(subjectList);
                college.setSubjectName(college.getCollegeName());
            }
            return ResultUtil.success(1,"正常",list);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"异常");
        }
    }


}